package org.atlantis.datasink;

import java.util.Collection;

import org.atlantis.datasink.codec.DeviceIdMapping;
import org.atlantis.datasink.codec.ICodec;
import org.atlantis.datasink.server.IServer;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringLauncher extends StandardLauncher implements ApplicationContextAware {

	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		Collection<IServer> servers = ctx.getBeansOfType(IServer.class).values();
		Collection<ICodec> codecs = ctx.getBeansOfType(ICodec.class).values();
		Collection<DeviceIdMapping> mappings = ctx.getBeansOfType(DeviceIdMapping.class).values();
		for (IServer server : servers)
			super.loadServer(server);
		for (ICodec codec : codecs)
			super.loadCodec(codec);
		if (mappings.size() > 0) super.loadDeviceIdMapping(mappings.toArray(new DeviceIdMapping[1])[0]);
	}
}
